package com.neusoft.weekreport.component.controller;

import com.neusoft.weekreport.component.bizService.UserBizService;
import com.neusoft.weekreport.component.req.*;
import com.neusoft.weekreport.component.resp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@EnableTransactionManagement
@RequestMapping("/user")
public class UserController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    UserBizService userBizService;

    //查询用户
    @RequestMapping(value = "/userManagement", method = RequestMethod.GET)
    public ResponseBean<GetUserInfoListResp>  getUserInfoList(GetUserInfoReq req) {
        ResponseBean<GetUserInfoListResp> responseBean = new ResponseBean<>();
        try{
            GetUserInfoListResp resp = userBizService.getUserInfoList(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询用户列表成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;

    }


    //添加用户-保存用户
    @RequestMapping(value = "userManagement",method = RequestMethod.POST)

    public ResponseBean<GetAddUserInfoResp> addUserInfoList(@RequestBody GetAddUserInfoReq req, HttpServletRequest request){
        ResponseBean<GetAddUserInfoResp> responseBean = new ResponseBean<>();
        try{
            GetAddUserInfoResp resp = userBizService.addUserInfoList(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"添加用户成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return  responseBean;
    }
    //根据id查看用户
    @RequestMapping(value = "viewUser",method = RequestMethod.GET)
    public ResponseBean<GetUserInfoByIdResp> getUserInfoById(GetUserInfoByIdReq req){
        ResponseBean<GetUserInfoByIdResp> responseBean = new ResponseBean<>();
        try{
            GetUserInfoByIdResp resp = userBizService.getUserInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"获取用户的信息成功",resp);

        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;

    }

    //修改用户-保存用户
    @RequestMapping(value = "userManagement",method = RequestMethod.PUT)
    public ResponseBean<GetAddUserInfoResp>  editUserInfoList(@RequestBody GetAddUserInfoReq req, HttpServletRequest request){
        ResponseBean<GetAddUserInfoResp> responseBean = new ResponseBean<>();
        try{
            GetAddUserInfoResp resp = new GetAddUserInfoResp();
            userBizService.editUserInfoList(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(), "修改用户成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //启用或者禁用用户操作
    @RequestMapping(value = "userStatus",method = RequestMethod.PUT)
    public ResponseBean<GetChangeUserStatusResp> changeUserStatus(@RequestBody GetChangeUserStatusReq req,HttpServletRequest request){
        ResponseBean<GetChangeUserStatusResp> responseBean = new ResponseBean<>();
        try{
            GetChangeUserStatusResp resp = new GetChangeUserStatusResp();
            userBizService.changeUserStatus(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(), "改变用户状态成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //删除用户操作
    @RequestMapping(value = "userManagement",method = RequestMethod.DELETE)
    public ResponseBean<GetChangeUserStatusResp> deleteUser(@RequestBody GetChangeUserStatusReq req,HttpServletRequest request){
        ResponseBean<GetChangeUserStatusResp> responseBean = new ResponseBean<>();
        try{
            GetChangeUserStatusResp resp = new GetChangeUserStatusResp();
            userBizService.deleteUser(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(), "删除用户成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //重置用户密码操作
    @RequestMapping (value = "userPwdReset",method = RequestMethod.POST)
    public ResponseBean<GetChangeUserStatusResp> userPwdReset(@RequestBody GetChangeUserStatusReq req,HttpServletRequest request){
        ResponseBean<GetChangeUserStatusResp> responseBean = new ResponseBean<>();
        try{
            GetChangeUserStatusResp resp = new GetChangeUserStatusResp();
            userBizService.userPwdReset(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(), "用户密码重置成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //修改密码
    @RequestMapping(value = "/modifyPassword",method = RequestMethod.PUT)
    public ResponseBean modifyPassword(@RequestBody GetModifyPasswordReq req, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            userBizService.modifyPassword(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(), "修改用户密码成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

    //如果是组长，返回组员，如果是组员，返回自己
    @RequestMapping(value ="/getGroupAfterUser",method = RequestMethod.GET)
    public ResponseBean<GetUserInfoAfterGroupResp> getGroupAfterUser(GetUserInfoByIdReq req, HttpServletRequest request){
        ResponseBean<GetUserInfoAfterGroupResp> responseBean = new ResponseBean();
        try {
            GetUserInfoAfterGroupResp resp = userBizService.getGroupAfterUser(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(), "查询组员成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
            logger.error(e.toString());
        }
        return responseBean;
    }

}
